<?php

function karyawan_resign($id){
	$query = "SELECT * FROM pemberhentian WHERE karyawan_id = {$id}";
	$res = my_query($query);
	if(my_num_rows($res) > 0 ){
		return true;
	}
	return false;
}
function list_data_karyawan(){
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'nik'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),   
		'nama'=>array('style'=>'border-bottom:2px solid;width:30%'),   
		'jabatan'=>array('style'=>'border-bottom:2px solid;width:35%'), 
	//	'direktorat'=>array('style'=>'border-bottom:2px solid;width:15%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:15%'), 
	);
	
	if(isset($_GET['key'])){
	$query = "
		SELECT * FROM karyawan a 
		INNER JOIN biodata b 
		ON a.biodata_id = b.biodata_id
		WHERE karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
		AND b.nama LIKE '%{$_GET['key']}%' OR a.nik ='{$_GET['key']}'
	";
	}else{
	$query = "
		SELECT * FROM karyawan a 
		INNER JOIN biodata b 
		ON a.biodata_id = b.biodata_id
		WHERE karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
	";
	}
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result )){
		$i++;
		 
		$detailproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=detail&id='.$ey['karyawan_id'],
						'title'=>'Detail'
				);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		
		$skoringproperty = array(
						'href'=>'index.php?com=karyawan&task=skoring&id='.$ey['karyawan_id'],
						'rel'=>'facebox',
						'title'=>'Hasil seleksi'
				);
		$skoring_button = button_icon( 'paste_on.gif' , $skoringproperty  );
		
		$kekaryawanan = my_query("
		SELECT * FROM jabatan a 
		INNER JOIN kekaryawanan c ON a.jabatan_id=c.jabatan_id
		WHERE c.karyawan_id ={$ey['karyawan_id']} LIMIT 1
		");
		$jbt_karya = my_fetch_array($kekaryawanan );
		 
		if(karyawan_resign($ey['karyawan_id'])){
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),
			'nik'=>position_text_align( '<font color="red">'. $ey['nik'] .'</font>', 'center'),
			'nama'=>'<font color="red">'.$ey['nama'],  
			'jabatan'=> '<font color="red">'.$jbt_karya['nama_jabatan'].'</font>',  
			'operasi'=> position_text_align( $detail_button .' '.$edit_button  , 'right'),  
		);
		}else{
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>position_text_align( $ey['nik'], 'center'),
			'nama'=>$ey['nama'],  
			'jabatan'=> $jbt_karya['nama_jabatan'],  
			'operasi'=> position_text_align( $detail_button .' '.$edit_button  , 'right'),  
		);
		}
	}
	
	$datas = table_rows($row); 
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" /><span style="font:10px verdana"><b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" /><input class="simple_search" type="submit" value=" CARI " /> </form>';
	$printmenu = additional_menu_on_list(array() , $search);
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu . table_builder($header , $datas ,  15 ,false , $paging  );
}

function list_potongan_by_karyawan_id($karyawan_id){
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),      
		'nama potongan'=>array('style'=>'border-bottom:2px solid;width:35%'),   
		'  mulai'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
		'  selesai'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
		'Potongan'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'), 
		'Frekuensi'=>array('style'=>'text-align:center;border-bottom:2px solid;width:15%'), 
		'Total'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),   
	
	);
	$query = "SELECT * FROM pr_potongan_limit a
	LEFT JOIN pr_komponen_gaji b ON a.komponen_gaji_id=b.komponen_gaji_id 
	WHERE a.karyawan_id = {$karyawan_id}
	ORDER BY a.potongan_limit_id DESC "; 
	$result = my_query($query);
	$row = array();
	$i=0;
	while( $ey = my_fetch_array($result) ){
		$i++;
		$row[] = array(
			'no'=>position_text_align( $i , 'center'),
			'nama'=> $ey['nama_komponen_gaji'],
			'tgl_mulai'=>position_text_align( $ey['tanggal_mulai'] , 'center'),
			'tgl_selesai'=>position_text_align( $ey['tanggal_selesai'] , 'center'),
			'pto'=>position_text_align(  rp_format($ey['potongan'] ), 'right'),
			'co'=>position_text_align( $ey['waktu'] , 'center'),
			'total'=>position_text_align( rp_format($ey['total']) , 'right'), 
		);
	}
	
	$datas = table_rows_plain($row); 
	$tombols = array(
		'Tambah data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com=pr_potongan_limit_karyawan&id='.$karyawan_id.'&task=edit&pid=0\' });')
	
	);
	$printmenu = additional_menu_on_list($tombols , '');
	return $printmenu . table_builder_plain($header , $datas ,  6 ,false    );
}

function display_data_potongan_by_karyawan_id($karyawan_id){
	
	$karyawans = loaddata_karyawan($karyawan_id);
	$view = karyawan_header_detail($karyawans , true);
	$view .='<br/>'. list_potongan_by_karyawan_id($karyawan_id);
	
	return $view;
}


function submit_potongan_limit($karyawan_id , $id = 0){
	$waktu=($_POST['tahun_s']*12+$_POST['bulan_s'])-($_POST['tahun']*12+$_POST['bulan'])+1;
	$total=set_text_as_number($_POST['total'])	;
	$potongan=$total/$waktu;

	$tanggal_mulai = $_POST['tahun'].'-'.$_POST['bulan'].'-01';
	$tanggal_selesai = $_POST['tahun_s'].'-'.$_POST['bulan_s'].'-01';	 
	$datas = array(
		'komponen_gaji_id'=>my_type_data_int($_POST['komponen_gaji_id']),
		'karyawan_id'=> my_type_data_int($karyawan_id),
		'tanggal_mulai'=> my_type_data_str($tanggal_mulai),
		'tanggal_selesai'=> my_type_data_str($tanggal_selesai),
		'total'=>my_type_data_str(set_text_as_number($_POST['total'])),
		'potongan'=> my_type_data_str($potongan),
		'waktu'=> my_type_data_int($waktu),
	);
	
	if($id > 0){
		return my_update_record( 'pr_potongan_limit' , 'potongan_limit_id' , $id , $datas);
	}
	
	return my_insert_record( 'pr_potongan_limit' , $datas );
}


function form_potongan_limit( $karyawan_id , $id ){
 
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);

	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js'  
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
			)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
		
		function checkValue(){
			var bulan = $(\'#bulan\').val();
			var xtahun = $(\'#tahun\').val();
			var bulan_end = $(\'#bulan_s\').val();
			var tahun_end = $(\'#tahun_s\').val(); 
			var total = $(\'#total\').val(); 
			var bulanan = $(\'#bulanan\').val(); 
			var frekuensi = $(\'#frekuensi\').val(); 
			 
			$.get("get_kalkulasi_potongan.php", { 
				bulan: bulan , 	tahun: xtahun , 	
				bulan_end: bulan_end , 	tahun_end: tahun_end , 	
				bulanan: bulanan , total: total,
				frekuensi: frekuensi
			},
			   function(data){
				    var t = data.split("|");
					//KOMPOSISI BSTART|YSTART|BULAN_END|TAHUN_END|FREKUENSI|TOTAL|NILAI_STOR_BULANAN
					$(\'#total\').val(t[5]); 
					$(\'#bulanan\').val(t[6]); 
					$(\'#frekuensi\').val(t[4]); 
					$(\'#bulan_s\').val(t[2]);
					$(\'#tahun_s\').val(t[3]); 
					alert(t[2]);
					return false;
				  
			   });
		}
	');	
	
	
	$view = form_header( "seleski" , "seleski"  );
	$karyawan_data = loaddata_karyawan($karyawan_id); 
	$view .= form_field_display( $karyawan_data['nik'].'/'.$karyawan_data['nama']  , "NIK/Nama Karyawan"  );
	if($id>0){
		$fields = my_get_data_by_id('pr_potongan_limit','potongan_limit_id',$id);
		$month = date('m',strtotime($fields['tanggal_mulai']));
		$year = date('Y',strtotime($fields['tanggal_mulai']));	
		$month_s = date('m',strtotime($fields['tanggal_selesai']));
		$year_s = date('Y',strtotime($fields['tanggal_selesai']));	

		}else{
		$month = date('m');
		$year = date('Y');
		$month_s = date('m');
		$year_s = date('Y');
	} 

	$gols = array();
	$query_golongan = "SELECT * FROM pr_komponen_gaji WHERE model_entry = 6";
	$re = my_query($query_golongan);
	 while($row = my_fetch_array($re) ){
		$gols[$row['komponen_gaji_id']] =$row['nama_komponen_gaji'];
	 }
	 $gol = array(
		'name'=>'komponen_gaji_id',
		'id'=>'komponen_gaji_id',
		'value'=>isset($_POST['komponen_gaji_id']) ? $_POST['komponen_gaji_id'] : $fields['komponen_gaji_id'] 
		);
	$form_field_gol = form_dropdown($gol , $gols);
	$view .= form_field_display( $form_field_gol   , "Spesifikasi");
 
	$bulans  = array(
		'01'=>'01', 
		'02'=>'02', 
		'03'=>'03', 
		'04'=>'04', 
		'05'=>'05', 
		'06'=>'06', 
		'07'=>'07', 
		'08'=>'08', 
		'09'=>'09', 
		'10'=>'10', 
		'11'=>'11', 
		'12'=>'12', 
	);
	$bulan = array(
		'name'=>'bulan',
		'value'=>isset($_POST['bulan']) ? $_POST['bulan']: $month  
	);
	$bulan_form = form_dropdown( $bulan,$bulans);
	
	$tahun = array(
		'name'=>'tahun','id'=>'tahun','size'=>5,
		'value'=>isset($_POST['tahun']) ? $_POST['tahun']: $year
	); 
	$tahun_form = form_dynamic($tahun );
	$view .= form_field_display( $bulan_form.' '. $tahun_form .'<input type="button" onclick="javascript:checkValue();"  value=" Chk "/>' , "Bayar pertama"    );
	
	$bulan_s = array(
		'name'=>'bulan_s','id'=>'bulan_s',
		'value'=>isset($_POST['bulan_s']) ? $_POST['bulan_s']: $month_s  
	);
	$bulan_s_form = form_dropdown( $bulan_s,$bulans);
	
	$tahun_s = array(
		'id'=>'tahun_s','name'=>'tahun_s','size'=>5,
		'value'=>isset($_POST['tahun_s']) ? $_POST['tahun_s']: $year_s
	); 
	$tahun_s_form = form_dynamic($tahun_s );
	$view .= form_field_display( $bulan_s_form.' '. $tahun_s_form  , "Bayar terakhir"    );

	$total = array(
				'name'=>'total',
				'value'=>( isset($_POST['total']) ? $_POST['total'] : $fields['total'])  ,
				'id'=>'total',
				'type'=>'text',
				'size'=>'20'
			);
	$form_field_total = form_dynamic($total); 
	$view .= form_field_display( $form_field_total   , "Total"   );
	$bulanan = array(
				'name'=>'bulanan',
				'value'=>( isset($_POST['bulanan']) ? $_POST['bulanan'] :  $fields['bulanan'] ),
				'id'=>'bulanan',
				'type'=>'text',
				'size'=>'20'
			);
	$form_field_bulanan= form_dynamic($bulanan); 
	$view .= form_field_display( $form_field_bulanan   , "Pot / bulan"   );
	$frekuensi = array(
				'name'=>'frekuensi',
				'value'=>( isset($_POST['frekuensi']) ? $_POST['frekuensi'] :  $fields['frekuensi']  ),
				'id'=>'frekuensi',
				'type'=>'text',
				'size'=>'5'
			);
	$form_field_frekuensi= form_dynamic($frekuensi); 
	$view .= form_field_display( $form_field_frekuensi   , "Frekuensi"   );
		
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}